package android.study.project3;

import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * <h3>Fancy Blurred Background Activity</h3>
 */
public class TranslucentBlurActivity extends Activity implements OnClickListener {

	private Button mBtnDisplaySettings;
	private Button mBtnWifiSettings;
	private Button mBtnGPSSettings;
	private Button mBtnBluetoothSettings;
	private Button mBtnBatterySettings;

	@Override
	protected void onCreate(Bundle icicle) {
		// Be sure to call the super class.
		super.onCreate(icicle);

		// Have the system blur any windows behind this one.
		if (!isLiveWallpaper(this) && BatteryWidget.SDK_VERSION < 11) // no blur on
																																	// honeycomb
		{
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
					WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		}

		setContentView(R.layout.settings);

		mBtnDisplaySettings = (Button) findViewById(R.id.displaySettings);
		mBtnDisplaySettings.setOnClickListener(this);

		mBtnWifiSettings = (Button) findViewById(R.id.wifiSettings);
		mBtnWifiSettings.setOnClickListener(this);

		WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
		mBtnWifiSettings.setText(wifiManager.isWifiEnabled() ? "Wifi" : "Wifi");
		mBtnWifiSettings.setTextColor(wifiManager.isWifiEnabled() ? 0xFF00D318 : Color.RED);

		mBtnGPSSettings = (Button) findViewById(R.id.gpsSettings);
		mBtnGPSSettings.setOnClickListener(this);

		mBtnBluetoothSettings = (Button) findViewById(R.id.bluetoothSettings);
		mBtnBluetoothSettings.setOnClickListener(this);

		if (BatteryWidget.SDK_VERSION > 3) {
			mBtnBatterySettings = (Button) findViewById(R.id.batterySettings);
			mBtnBatterySettings.setOnClickListener(this);
		}

		try {
			LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
			boolean gpsOn = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
			mBtnGPSSettings.setText(gpsOn ? "GPS" : "GPS");
			mBtnGPSSettings.setTextColor(gpsOn ? 0xFF00D318 : Color.RED);

			int btStatus = Settings.Secure.getInt(getContentResolver(),	android.provider.Settings.Secure.BLUETOOTH_ON);
			mBtnBluetoothSettings.setTextColor(btStatus == 0 ? Color.RED : 0xFF00D318);
		} catch (Exception e) {
			Log.d("BatteryWidget", "Failed to get Bluetooth status", e);
		}

		((RelativeLayout) findViewById(R.id.settingsLayout))
				.startAnimation(AnimationUtils.loadAnimation(this, R.anim.launchanim));
	}

	protected void onPause() {
		super.onPause();
	}

	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	public void onClick(View v) {

		try {
			if (v == mBtnDisplaySettings) {
				Intent defineIntent2 = new Intent("com.android.settings.DISPLAY_SETTINGS");
				defineIntent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				defineIntent2.addCategory("android.intent.category.DEFAULT");
				startActivity(defineIntent2);
				finish();
			} else if (v == mBtnWifiSettings) {
				WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
				boolean enabled = wifiManager.isWifiEnabled();
				wifiManager.setWifiEnabled(!enabled);

				Toast.makeText(this, enabled ? "Disabling Wifi" : "Enabling Wifi", Toast.LENGTH_SHORT).show();
				finish();
			} else if (v == mBtnGPSSettings) {
				Intent defineIntent2 = new Intent("android.settings.LOCATION_SOURCE_SETTINGS");
				defineIntent2.addCategory("android.intent.category.DEFAULT");
				defineIntent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(defineIntent2);
				finish();
			} else if (v == mBtnBluetoothSettings) {
				Intent defineIntent2 = new Intent("android.settings.WIRELESS_SETTINGS");
				defineIntent2.addCategory("android.intent.category.DEFAULT");
				defineIntent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(defineIntent2);
				finish();
			} else if (v == mBtnBatterySettings) {
				Intent defineIntent2 = new Intent("android.intent.action.POWER_USAGE_SUMMARY");
				startActivity(defineIntent2);
				finish();
			}
		} catch (Exception e) {
			Log.e("BatteryWidget", "Settings Error", e);
		}
	}

	public static boolean isLiveWallpaper(Context context) {
		boolean liveWallpaper = false;
		try {
			if (BatteryWidget.SDK_VERSION >= 7) {
				// this version of code needs Android 2.0, but we want to run on 1.5+,
				// so use reflection
				// this version of code works on all vers of android
				Class classWallpaperManager = Class.forName("android.app.WallpaperManager");
				if (classWallpaperManager != null) {
					Method methodGetInstance = classWallpaperManager.getDeclaredMethod("getInstance", Context.class);
					Object objWallpaperManager = methodGetInstance.invoke(classWallpaperManager, context);

					Method methodGetWallpaperInfo = objWallpaperManager.getClass().getMethod("getWallpaperInfo", null);
					Object objWallPaperInfo = methodGetWallpaperInfo.invoke(objWallpaperManager, null);
					if (objWallPaperInfo != null) {
						liveWallpaper = true;
					}
				}
			}
		} catch (Throwable t) {
		}

		return liveWallpaper;
	}
}
